<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true"  label-width="68px">
      <el-form-item label="收费站" prop="deptId" label-width="100px" v-if="deptId==101">
        <el-tree-select v-model="queryParams.deptId" :data="deptOptions" :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
          placeholder="请选择收费站" check-strictly clearable />
      </el-form-item>
      <el-form-item label="月份" prop="yearMonth">
        <el-date-picker v-model="queryParams.yearMonth" type="month" value-format="YYYY-MM" placeholder="请选择" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['coordination:etc_public:add']">上报</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['coordination:etc_public:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shuntingList" @selection-change="handleSelectionChange" show-overflow-tooltip>
      <el-table-column type="index" label="序号" width="65" align="center" />
      <el-table-column label="收费站" align="center" prop="deptName"  width="130" />
      <el-table-column label="月份" align="center" prop="yearMonth"  width="110">
        <template #default="scope">
          <span>{{ parseTime(scope.row.yearMonth, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="新办" align="center">
        <el-table-column label="正常发行" align="center" prop="">
          <el-table-column label="客车" align="center" prop="newNoCar" />
          <el-table-column label="货车" align="center" prop="newNoTruck" />
        </el-table-column>
        <el-table-column label="晋保通" align="center" prop="vehicleType">
          <el-table-column label="客车" align="center" prop="newJbtCar" />
          <el-table-column label="货车" align="center" prop="newJbtTruck" />
        </el-table-column>
      </el-table-column>
      <el-table-column label="免更" align="center" prop="freeChange" />
      <el-table-column label="激活(升级)" align="center" prop="activation" />
      <el-table-column label="充值" align="center">
        <el-table-column label="单数" align="center" prop="payNum" />
        <el-table-column label="金额(元)" align="center" prop="payAmount" />
      </el-table-column>

      <el-table-column label="注销" align="center" prop="logout" />
      <el-table-column label="备注" align="center" prop="remark" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['coordination:etc_public:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['coordination:etc_public:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog title="ETC上报" v-model="open" width="1000px" append-to-body>
      <el-form ref="shuntingRef" :model="form" :rules="rules" label-width="120px" class="freeFormItem">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="收费站" prop="deptId">
              <el-tree-select style="width: 100%;" v-model="form.deptId" :data="deptOptions" :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
              placeholder="请选择收费站" check-strictly clearable />
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="月份" prop="yearMonth">
              <el-date-picker style="width: 100%;"  v-model="form.yearMonth" type="month" value-format="YYYY-MM" placeholder="请选择" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="正常发行客车" prop="newNoCar">
              <el-input v-model="form.newNoCar" placeholder="请输入正常发行客车" style="width:100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="正常发行货车" prop="newNoTruck">
              <el-input v-model="form.newNoTruck" placeholder="请输入正常发行货车" style="width:100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="晋保通客车" prop="newJbtCar">
              <el-input v-model="form.newJbtCar" placeholder="请输入晋保通客车" style="width:100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="晋保通货车" prop="newJbtTruck">
              <el-input v-model="form.newJbtTruck" placeholder="请输入晋保通货车" style="width:100%" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="免更" prop="freeChange">
              <el-input v-model="form.freeChange" placeholder="请输入免更" style="width:100%" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="激活（升级）" prop="activation">
              <el-input v-model="form.activation" placeholder="请输入" style="width:100%" />
            </el-form-item>
          </el-col>


          <el-col :span="12">
            <el-form-item label="充值单数" prop="payNum">
              <el-input v-model="form.payNum" placeholder="请输入充值单数" style="width:100%" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="充值金额（元）" prop="payAmount">
              <el-input v-model="form.payAmount" placeholder="请输入充值金额" style="width:100%" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="注销" prop="logout">
              <el-input v-model="form.logout" placeholder="请输入" style="width:100%" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入" style="width:100%" rows="6" />
            </el-form-item>
          </el-col>

        </el-row>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Shunting">
import moment from 'moment'
import { listEtc_public, getEtc_public, delEtc_public, addEtc_public, updateEtc_public } from "@/api/coordination/etc_public";
import { getCKInfo } from '@/utils/auth'
import { deptTreeSelect } from "@/api/system/user";
import useUserStore from '@/store/modules/user'
const { proxy } = getCurrentInstance();
const { shunting_type } = proxy.useDict('shunting_type');
const shuntingList = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");
const radio1 = ref("调车管理")
const vehicleList = ref([])
const deptOptions = ref(undefined);
const deptId=ref(Number(getCKInfo('deptId')))
const userStore = useUserStore()
const userDeptName = ref("")

const data = reactive({
  form: { shuntingType: 1 },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deptId:deptId.value,
    yearMonth:moment().format('YYYY-MM')
  },
  rules: {

  }
});

const { queryParams, form, rules } = toRefs(data);



/** 查询调车明细列表 */
function getList() {
  loading.value = true;
  if(deptId.value==101||deptId.value==100){
    queryParams.value.deptId=''
  }
  listEtc_public(queryParams.value).then(response => {
    shuntingList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then(response => {
    deptOptions.value = response.data;
  });
};

const bindChangeVehicleNo = (val) => {


}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    deptId:deptId.value
  };
  proxy.resetForm("shuntingRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}


/** 新增按钮操作 */
function handleAdd() {
  form.value.deptId = deptId.value;
  open.value = true;
  title.value = "添加ETC发行量完成情况";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getEtc_public(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改ETC发行量完成情况";
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除ETC发行量完成情况编号为"' + _ids + '"的数据项？').then(function() {
    return delEtc_public(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["shuntingRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateEtc_public(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addEtc_public(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}



/** 导出按钮操作 */
function handleExport() {
  proxy.download('coordination/etc_public/export', {
    ...queryParams.value
  }, `ETC发行量完成情况_${new Date().getTime()}.xlsx`)
}

getDeptTree();
getList()
</script>


<style scoped>
.margin-bottom {
  margin-bottom: 15px;
}

.respon {
  width: 100%;
}
.freeFormItem .el-form-item.el-form-item--default.asterisk-left {
  display: flex;
}
.el-form--default.el-form--label-top .el-form-item .el-form-item__labe {
  width: 100px;
  flex-shrink: 0;
}
</style>